// pages/homeBrand/homeBrand.js
var app = getApp;
const router = require('../../../config').router
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: [{
      img: '../../../image/banner1.png',
      content: '品牌农产品'
    }],
    list: [{
      img: '../../../image/home_list_1.png',
      title: '苏省农垦集团米业公司',
      content: '农垦稻米',
      date: '2018-08-24 17:50'
    }, {
      img: '../../../image/home_list_1.png',
      title: '苏省农垦集团米业公司',
      content: '农垦稻米',
      date: '2018-08-24 17:50'
    }],
    bList: [
      {
        imageUrl: '../../../image/homeBrand_1.png',
        name: '粮油干货',
        id: '1'
      }, {
        imageUrl: '../../../image/homeBrand_2.png',
        name: '畜禽蛋类',
        id: '2'
      }, {
        imageUrl: '../../../image/homeBrand_3.png',
        name: '精品果蔬',
        id: '3'
      }, {
        imageUrl: '../../../image/homeBrand_4.png',
        name: '创意农产',
        id: '4'
      }, {
        imageUrl: '../../../image/homeBrand_5.png',
        name: '茶叶保健',
        id: '5'
      }, {
        imageUrl: '../../../image/homeBrand_6.png',
        name: '休闲食品',
        id: '6'
      }, {
        imageUrl: '../../../image/homeBrand_7.png',
        name: '海鲜水产',
        id: '7'
      }, {
        imageUrl: '../../../image/homeBrand_8.png',
        name: '特色美食',
        id: '8'
      }, {
        imageUrl: '../../../image/homeBrand_9.png',
        name: '更多',
        id: '9'
      }
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    windowHeight:'',
    windowWidth:'',
    keyword: '',
    voteTitle: '',
    //页数
    lastPage: 0,
    pageNo: 1,
    name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //品牌广告接口 轮播图
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'ad.query.brandAd',
        v: '1.0',
        format: 'json',
        categoryId:90
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          var banners = res.data.ad
          that.setData({
            banners: banners
          })
        }
      }
    })
    //相关品牌
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'institutions.category.brand.recursion',
        v: '1.0',
        format: 'json',
        categoryId:87
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          that.setData({
            bList: res.data.category,
            name: res.data.category[0].name
          })
          //请求品牌农产品          
          wx.request({
            url: router,
            data: {
              appKey: 'w4q897jgvxkb',
              method: 'article.list.brand',
              v: '1.0',
              format: 'json',
              categoryId: that.data.bList['0'].id
            },
            success: function (data) {
              if (data.statusCode == 200 && data.data.error != 'error') {
                var list = data.data.article
                that.setData({
                  list: list,
                  lastPage: data.data.page.last,
                  categoryId: that.data.bList['0'].id
                })
              }
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  imageError(e) {
    var that = this
    var height = e.detail.height;
    var width = e.detail.width;
    var ratio = width / height
    var height1 = parseInt(that.data.windowWidth / ratio);
    console.log(height1)
    that.setData({
      height: height1 + 'px'
    })
  },
  /**
   * bList
   */
  bList: function(e) {
    var id = e.currentTarget.dataset.idx;
    // console.log(id)
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
  //searchClick搜索
  searchClick: function (e) {
    var that = this;
    var keyword = that.data.keyword;
    var method = '';
    if (keyword == '') {
      wx.showModal({
        title: "提示",
        content: "请输入关键字！",
        showCancel: false,
        confirmText: "确定"
      })
    } else {
      wx.request({
        url: router,
        data: {
          method: 'article.search',
          appKey: 'w4q897jgvxkb',
          v: '1.0',
          format: 'json',
          searchword: keyword
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            if (res.data.article) {
              that.setData({
                list: res.data.article
              })
            } else {
              wx.showModal({
                title: "提示",
                content: "暂无搜索结果！",
                showCancel: false,
                confirmText: "确定"
              })
            }
          }
        }
      });
    }
  },
  //获取input值
  voteTitle: function (e) {
    var that = this;
    var value = e.detail.value;
    // console.log(e.detail.value);
    that.setData({
      keyword: value
    })
  },
  //点击切换品牌
  change(e){
    var that = this;
    var categoryId = e.currentTarget.dataset.idx;
    var key = e.currentTarget.dataset.key;
    //品牌农产品
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'article.list.brand',
        v: '1.0',
        format: 'json',
        categoryId: categoryId
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          var list = res.data.article
          that.setData({
            list: list,
            lastPage: res.data.page.last,
            categoryId: categoryId,
            pageNo:1,
            name: that.data.bList[key].name
          })
        }
      }
    })
  },
  //上拉加载
  onReachBottom() {
    var that = this;
    var p = that.data.pageNo
    var navbarid = that.data.navbarid
    p++
    var last = that.data.lastPage
    if (p <= last) {
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.brand',
          v: '1.0',
          format: 'json',
          categoryId: that.data.categoryId,
          pageNo: p
        },
        success: function (data) {
          if (data.statusCode == 200 && data.data.error != 'error') {
            // console.log(data.data)
            for (var i = 0; i < data.data.article.length; i++) {
              that.data.list.push(data.data.article[i])
            }
            setTimeout(function () {
              that.setData({
                list: that.data.list,
                pageNo: data.data.page.pageNo
              })
            }, 1000)
          }
        }
      })
    } else {
      wx.showToast({
        title: '没有更多。。',
        icon: 'loading',
        duration: 1500
      })
    }
  },
  /**
   * 详情
   */
  articleClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  },
})